<?php
/*
	插件概览
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	$item=array();
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'set',
		'desc'=>'自定义导出',
		'edit'=>array(
			array('type'=>'radio','title'=>'导出类型','name'=>'type','options'=>'type'),
			array('type'=>'string','title'=>'订单号','name'=>'ordersn'),
			array('type'=>'radio','title'=>'订单状态','name'=>'status','options'=>'status'),
			array('type'=>'radio','title'=>'支付方式','name'=>'paytype','options'=>'paytype'),
			array('name'=>'createtime','title'=>'下单时间','type'=>'time_range'),
			array('type'=>'checkbox','title'=>'自定义导出数据','name'=>'diy','options'=>'diy'),
			array('type'=>'checkbox','title'=>'商品导出数据','name'=>'diygoods','options'=>'diygoods'),
		)
	);
	$return['type']=array('0'=>'按订单导出','1'=>'按商品导出');
	$return['paytype']=array('wechat'=>'微信支付','credit'=>'余额支付');
	$return['status']=array('1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭');
	$return['diygoods']=array('goodssn'=>'编码','productsn'=>'国家商品条码','offcode'=>'商品称码','title'=>'商品名称','unit'=>'单位','price'=>'单价','total'=>'数量');
	$return['diy']=array('ordersn'=>'订单号','nickname'=>'买家昵称','phone'=>'买家电话','mid'=>'会员id','address'=>'收货地址','realname'=>'收货人','mobile'=>'收货电话','goods'=>'商品信息','paytype'=>'支付方式','goodsprice'=>'商品小计','discountprice'=>'运费','createtime'=>'下单时间','paytime'=>'付款时间','sendtime'=>'发货时间','finishtime'=>'订单完成时间','express'=>'快递公司','expresssn'=>'快递单号','remark'=>'订单备注');
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='set'){
	$post=get_postdata();	
	$_SESSION['e2cpost']=$post;
	$url=download_url('jy_weishop/order.export/down');
	exi('点确定开始下载','','',$url);
	
}
if($_RQ['op']=='down'){
	$f=array('ordersn'=>'订单号','nickname'=>'买家昵称','phone'=>'买家电话','mid'=>'会员id','address'=>'收货地址','realname'=>'收货人','mobile'=>'收货电话','goods'=>'商品信息','paytype'=>'支付方式','goodsprice'=>'商品小计','discountprice'=>'运费','createtime'=>'下单时间','paytime'=>'付款时间','sendtime'=>'发货时间','finishtime'=>'订单完成时间','express'=>'快递公司','expresssn'=>'快递单号','remark'=>'订单备注');
	$gf=array('goodssn'=>'编码','productsn'=>'国家商品条码','offcode'=>'商品称码','title'=>'商品名称','unit'=>'单位','price'=>'单价','total'=>'数量');
	$diy=str_array($_SESSION['e2cpost']['diy']);
	$diygoods=str_array($_SESSION['e2cpost']['diygoods']);
	$type=$_SESSION['e2cpost']['type'];
	$header=array();
	foreach($diygoods as $gv){
		if($gv){
			$header[$gv]=$gf[$gv];
		}
	}
	foreach($diy as $v){
		if($v){
			$header[$v]=$f[$v];
		}
	}
	if(!empty($_SESSION['e2cpost']['ordersn'])){
		$params['ordersn']=trim($_SESSION['e2cpost']['ordersn']);
	}
	if(!empty($_SESSION['e2cpost']['status'])){
		$params['status']=intval($_SESSION['e2cpost']['status']);
	}
	if(!empty($_SESSION['e2cpost']['paytype'])){
		$params['paytype']=intval($_SESSION['e2cpost']['paytype']);
	}
	//创建时间
	if($_SESSION['e2cpost']['createtime']){
		//$time_range=cfc('tools')->base64_decode($_SESSION['e2cpost']['createtime'],$_SESSION['base_key_str']);
		//$time_range=json_decode($time_range,true);
		$time_range=$_SESSION['e2cpost']['createtime'];
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$list=pdo_getall('jy_weishop_order',$params);
	$status=array('1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭');
	$paytype=array('wechat'=>'微信支付','credit'=>'余额支付');
	$ispay=array('0'=>'未支付','1'=>'已支付');
	if($type==1){
		$exdata=array();
	}
	foreach($list as &$v){
		$v['nickname']=pdo_getcolumn('core_members',array('id'=>$v['mid']),'nickname');
		$v['phone']=pdo_getcolumn('core_members',array('id'=>$v['mid']),'mobile');			
		$address=pdo_get('jy_weishop_address',array('id'=>$v['addressid'],'uniacid'=>$_SESSION['uniacid']));
		$v['address']=$address['province'].$address['city'].$address['address'];
		$v['mobile']=$address['mobile'];
		$v['realname']=$address['realname'];
		$q=$v['status'];
		$v['status']=$status[$q];
		$w=$v['paytype'];
		$v['paytype']=$paytype[$w];
		$e=$v['ispay'];
		$v['ispay']=$ispay[$e];
		if(!$v['nickname']){
			$v['nickname']='匿名用户';
		}
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
		$v['payime']=date('Y-m-d H:i:s',$v['payime']);
		$v['sendtime']=date('Y-m-d H:i:s',$v['sendtime']);
		$v['finishtime']=date('Y-m-d H:i:s',$v['finishtime']);
		$v['storetitle']=pdo_getcolumn('jy_weishop_store',array('id'=>$v['storeid']),'title');
		$v['storetitle']=$v['storetitle']?:'无';		
		$goods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$v['id']),array('goodsid','price','total'));
		foreach($goods as $k=>$gv){
				$goodsinfo=pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid']),array('title','goodssn','productsn','offcode','unit'));
				$goodsinfo['title']=str_replace(',',' ',$goodsinfo['title']);
				$goodstitle=$goodsinfo['title'];
				//商品规格
				if($gv['optionid']){
						$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$gv['optionid']),'specs');
						$specs=explode("_",$specs);
						foreach($specs as &$spec){
							$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
						}
						$selespename=implode("，",$specs);
				}else{
					$selespename='无规格';
				}
				if($type==1){
					$newv['title']='['.$selespename.']'.$goodsinfo['title'];
					$newv['goodssn']=$goodsinfo['goodssn']?:'无';
					$newv['productsn']=$goodsinfo['productsn']?:'无';
					$newv['offcode']=$goodsinfo['offcode']?:'无';
					$newv['unit']=$goodsinfo['unit']?:'件';
					$newv['price']=$gv['price'];
					$newv['total']=$gv['total'];
					$newv=array_merge($v,$newv);
					$exdata[]=$newv;
				}else{
					$num=$k+1;
					$v['goods'] .='【商品'.$num.'】['.$selespename.']'.$goodstitle;
				}
		}
	}
	$exdata=$type==1?$exdata:$list;
	cfc('tools')->export2excel($header,$exdata,'订单');
}